class BaseModel<T> {
  final int code;
  final String message;
  final T? data;
  final List<T>? dataList;
  final BaseListPage<T>? dataPage;

  BaseModel({
    required this.code,
    this.data,
    required this.message,
    this.dataList,
    this.dataPage,
  });

  factory BaseModel.fromJson(
      Map<String, dynamic> json, T Function(dynamic json) fromJsonT) {
    // var model = jsonDecode(json);
    var code = json["code"] as int?;
    var message = json["message"] as String?;
    var data = json["data"] != null ? fromJsonT(json['data']) : null;
    var dataList = json['dataList'] != null
        ? (json['dataList'] as List).map<T>((e) => fromJsonT(e)).toList()
        : null;
    var dataPage = json['dataPage'] != null
        ? BaseListPage<T>.fromJson(json['dataPage'], fromJsonT)
        : null;
    return BaseModel(
      code: code ?? -1,
      data: data,
      dataList: dataList,
      dataPage: dataPage,
      message: message ?? "unknown message",
    );
  }
}

class BaseListPage<T> {
  final List<T>? list;
  final T? data;
  final int? total;
  final int? page;
  final int? pageSize;

  BaseListPage({
    this.list,
    this.data,
    this.total,
    this.page,
    this.pageSize,
  });

  factory BaseListPage.fromJson(
    Map<String, dynamic> json,
    T Function(dynamic json) fromJsonT,
  ) {
    return BaseListPage<T>(
      list: json['list'] != null
          ? (json['list'] as List<dynamic>?)?.map((e) => fromJsonT(e)).toList()
          : null,
      data: json['data'] != null ? fromJsonT(json['data']) : null,
      total: json['total'] as int?,
      page: json['page'] as int?,
      pageSize: json['pageSize'] as int?,
    );
  }

  Map<String, dynamic> toJson(Map<String, dynamic> map, Function(T) toJsonT) {
    return {
      if (list != null) "list": list!.map((e) => toJsonT(e)).toList(),
      if (data != null) 'data': toJsonT(data as T),
      'total': total,
      'page': page,
      'pageSize': pageSize,
    };
  }
}
